package com.gt.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * redis配置
 *
 * @author mkt
 */@Slf4j
@Component
@Configuration
public class RedisConfig {
    @Autowired
    private Environment env;
    @Value("${spring.profiles.active}")
    private String active;

    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("redis注入");
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
//    @Bean
//    public Redisson redisson() {
//        Config config = new Config();
//        if ("local".equals(active) || "test".equals(active)) {
//            String hostName = env.getProperty("spring.redis.host");
//            String port = env.getProperty("spring.redis.port");
//            String password = env.getProperty("spring.redis.password");
//            config.useSingleServer().setAddress("redis://" + hostName + ":" + port);
//            config.useSingleServer().setPassword(password);
//        }else {// 下云以后如果用机房里的哨兵，则放开注释
//            String sentinelMaster = env.getProperty("spring.redis.sentinel.master");
//            String sentinelNodes = env.getProperty("spring.redis.sentinel.nodes");
//            String sentinelPassword = env.getProperty("spring.redis.sentinel.password");
//            String password = env.getProperty("spring.redis.password");
//            config.useSentinelServers()
//                    .setMasterName(sentinelMaster)
//                    .addSentinelAddress(splitNodes(sentinelNodes))
//                    .setSentinelPassword(sentinelPassword)
//                    .setPassword(password)
//                    .setCheckSentinelsList(false);
//        }
//        return (Redisson) Redisson.create(config);
//    }
    public String[] splitNodes(String nodes) {
        String[] nodeArray = nodes.split(",");
        for (int i = 0; i < nodeArray.length; i++) {
            nodeArray[i] = "redis://" + nodeArray[i];
        }
        return nodeArray;
    }
}
